﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.Framework;
using Ubatic.Contracts.Contracts;
using Ubatic.ServiceFactory;
using Ubatic.Contracts.Services;
using Ubatic.UI.Code;

namespace Ubatic.UI.App
{
    public partial class ModificarUsuarios : BasePage
    {
        private const string pagDetalle = "/App/UsuarioDetalle.aspx";

        private IUserService UserService = Factory.GetUserService();
        private IListService ListService = Factory.GetListService ();

        protected void Page_Load(object sender, EventArgs e)
        {
            //HasPermissionToSeeMe(Operaciones.UsuarioDetalle);
            if (!this.IsPostBack)
            {
                LoadCbos();
                this.Buscar();
            }
        }

        private void LoadCbos()
        {
            var tipoDocumentoList = ListService.ObtenerTiposDocumento();
            UIHelper.LoadCbo(tipoDocumentoList, this.ddlTipoDocumento, "Todos");

            var dependenciasList = ListService.ObtenerDependencias();
            UIHelper.LoadCbo(dependenciasList, this.ddlUnidadAcademica, "Todas");
        }

        private void Buscar()
        {
            // cargo los filtros            
            CriteriaUsuarioDto criteria = new CriteriaUsuarioDto();

            string cuil = string.Concat(this.txtCuitA.Text.Trim(),
                                        this.txtCuitB.Text.Trim(),
                                        this.txtCuitC.Text.Trim());
            if (!string.IsNullOrEmpty(cuil))
                criteria.Cuil = long.Parse(cuil);


            criteria.Nombre = this.txtNombre.Text.Trim();
            criteria.Apellido = this.txtApellido.Text.Trim();
            criteria.TipoDocumentoId = UIHelper.GetIntFromInputCbo(this.ddlTipoDocumento);
            criteria.NroDocumento = UIHelper.GetLongFromInputText(this.txtNroDocumento.Text.Trim());
            criteria.DependenciaId = UIHelper.GetLongFromInputCbo(this.ddlUnidadAcademica);
            criteria.CorreoElectronico = this.txtCorreoElectronico.Text.Trim();

            // obtengo la lista
            List<UsuarioDto> list = Factory.GetUserService().ObtenerUsuarios(criteria);
            this.lblCantReg.Text = string.Format(" ({0})", list.Count);
            this.Grid.DataSource = list;
            this.Grid.DataBind();
        }

        protected void Grid_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                this.Grid.PageIndex = e.NewPageIndex;

                Buscar();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void Grid_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                long usuarioId = Convert.ToInt32(this.Grid.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
                if (e.CommandName == "editar")
                {
                    Navigation.NavigateTo(string.Format("{0}?Id={1}", pagDetalle, usuarioId));
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        private List<TipoDocumentoDto> _tiposDocumento;
        private List<TipoDocumentoDto> TipoDocumento
        {
            get
            {
                if (_tiposDocumento == null)
                {
                    _tiposDocumento = ListService.ObtenerTiposDocumento();
                }

                return _tiposDocumento;
            }
        }

        protected void Grid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    UsuarioDto cliente = (UsuarioDto)e.Row.DataItem;
                    var tipoDoc = TipoDocumento.Where(t => t.Id == cliente.TipoDocumentoId).Single();

                    e.Row.Cells[3].Text = tipoDoc.Nombre;
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void btnBuscar_Click(object sender, EventArgs e)
        {
            this.Buscar();
        }
    }
}